package demo2;

import java.io.*;
import java.net.*;

public class MyServer1 {
    public static void main(String[] args) {
        try{
            ServerSocket server = new ServerSocket(1680);
            Socket socket = server.accept();

            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));

            System.out.println("服务器已启动，等待客户端消息...");
            String s;
            while(( s = in.readLine()) != null){
                String[] s1 = s.split(" ");
                System.out.println("收到客户端求面积的图形是：" + s1[0]);

                Shape shape;

                if (s1[0].equals("rectangle")) {
                    System.out.printf("长:%s \t 宽:%s\n",s1[1],s1[2]);
                    double l = Double.parseDouble(s1[1]);
                    double w = Double.parseDouble(s1[2]);
                    shape = new Rectangle(w ,l);
                } else if (s1[0].equals("triangle")) {
                    System.out.printf("第一条边:%s \t 第二条边:%s \t 第三条边:%s\n",s1[1],s1[2],s1[3]);
                    double a = Double.parseDouble(s1[1]);
                    double b = Double.parseDouble(s1[2]);
                    double c = Double.parseDouble(s1[3]);
                    shape = new Triangle(a,b,c);
                } else {
                    System.out.printf("半径是:%s\n",s1[1]);
                    double r = Double.parseDouble(s1[1]);
                    shape = new Circle(r);
                }

                if("bye".equalsIgnoreCase(s)){
                    System.out.println("客户端请求关闭连接");
                    break;
                }

                double res = shape.getArea();
                String response = Double.toString(res);
                out.println(response);

                if ("bye".equalsIgnoreCase(response)) {
                    System.out.println("服务器主动关闭连接...");
                    break;
                }
            }

            System.out.println("连接正在关闭....");
            in.close();
            out.close();
            socket.close();
            server.close();
        } catch (Exception e) {
            throw new RuntimeException("服务器异常", e);
        }
    }
}


abstract class Shape{
    public abstract double getArea();
    public abstract double getPerimeter();
}

class Triangle extends Shape{
    double a;
    double b;
    double c;

    public Triangle() {
    }

    public Triangle(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    @Override
    public double getArea() {
        double p = (a + b + c) / 2;
        return Math.sqrt(p * (p - a) * (p - b) * (p - c));
    }

    @Override
    public double getPerimeter() {
        return this.a + this.b + this.c;
    }
}

class Circle extends Shape{
    double r;

    public Circle() {
    }

    public Circle(double r) {
        this.r = r;
    }

    @Override
    public double getArea() {
        return 3.10 * r * r;
    }

    @Override
    public double getPerimeter() {
        return 2 * 3.14 * r;
    }
}

class Rectangle extends Shape{
    double w;
    double l;
    public Rectangle() {
    }

    public Rectangle(double w, double l) {
        this.w = w;
        this.l = l;
    }

    @Override
    public double getArea() {
        return l * w;
    }

    @Override
    public double getPerimeter() {
        return 2 * (l + w);
    }
}
